package com.jizhangben.dao;

import com.jizhangben.pojo.UserPojo;
import com.jizhangben.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{
    public UserDaoImpl(){
    }

    @Override
    public UserPojo login(String userId, String loginPassword) {
        try {
            Connection connection = JdbcUtils.getConnection();
            String loginSql = "select  * from  users where id=? and password=?;";
            PreparedStatement preparedStatement = connection.prepareStatement(loginSql);
            preparedStatement.setString(1, userId);
            preparedStatement.setString(2, loginPassword);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {  // 查询不到用户数据
                return null;
            }
            // 将db中数据 返回给客户端 查询到数据
            Integer id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String password = resultSet.getString(3);
            UserPojo userPojo = new UserPojo(id, name, password);
            return userPojo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    @Override
    public UserPojo findByUserId(String userId) {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();
            String loginSql = "select  * from  users where id=?";
            preparedStatement = connection.prepareStatement(loginSql);
            preparedStatement.setString(1, userId);
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {  // 查询不到用户数据
                return null;
            }
            // 将db中数据 返回给客户端 查询到数据
            int id = resultSet.getInt(1);
            String dbUserName = resultSet.getString(2);
            String dbUserPwd = resultSet.getString(3);
            UserPojo UserPojo = new UserPojo(id, dbUserName, dbUserPwd);
            return UserPojo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }
    }
    @Override
    public int register(String userId,String userName, String userPwd) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnection();
            // sql语句写的操作 ----加上事务
            JdbcUtils.beginTransaction(connection); // 开启事务
            String insertSql = "INSERT INTO `jizhangben`.`users` (`id`, `name`, `password`) VALUES (?, ?, ?);";
            preparedStatement = connection.prepareStatement(insertSql);
            preparedStatement.setInt(1, Integer.parseInt(userId));
            preparedStatement.setString(2, userName);
            preparedStatement.setString(3, userPwd);
            int result = preparedStatement.executeUpdate();
            // 代码执行没有问题的情况下 则会提交数据
            JdbcUtils.commitTransaction(connection); // 提交事务
            return result;
        } catch (Exception e) {
            // 程序代码报错之后 是需要回滚事务
            e.printStackTrace();
            JdbcUtils.rollBackTransaction(connection);// 回滚事务
            return 0;
        } finally {
            JdbcUtils.closeConnection(preparedStatement, connection);
        }

    }
}


